{% extends "base.html" %}
{% set active_page = "admin_users" %}
{% if create %}
    {% set action_label = 'Create' %}
    {% set form_action = url_for('admin.edit_user') %}
{% else %}
    {% set action_label = 'Edit' %}
    {% set form_action = url_for('admin.edit_user', user_username=user.username) %}
{% endif %}
{% block title %}<title>{{ action_label }} User - {{ SITE_NAME }}</title>{% endblock %}

{% block dashboard_stat %}
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0 text-dark">{{ action_label }} User</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="{{ url_for('dashboard.dashboard') }}">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="{{ url_for('admin.manage_user') }}">Users</a></li>
                        <li class="breadcrumb-item active">{{ action_label }} User</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block content %}
    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12 col-sm-6 col-lg-4">
                    <form role="form" method="post" action="{{ form_action }}">
                        <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
                        <input type="hidden" name="create" value="{{ create }}">
                        <div class="card card-outline card-primary shadow">
                            <div class="card-header with-border">
                                <h3 class="card-title">User Editor</h3>
                            </div>
                            <!-- /.card-header -->
                            <div class="card-body">
                                {% if error %}
                                    <div class="alert alert-danger alert-dismissible">
                                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">
                                            &times;
                                        </button>
                                        <h4><i class="fa-solid fa-ban"></i> Error!</h4>
                                        {{ error }}
                                    </div>
                                    <span class="help-block">{{ error }}</span>
                                {% endif %}
                                <div class="form-group has-feedback">
                                    <label class="control-label" for="firstname">First Name</label>
                                    <input type="text" class="form-control" placeholder="First Name" name="firstname"
                                           id="firstname"
                                           {% if user %}value="{{ user.firstname }}" {% endif %}>
                                    <span class="form-control-feedback"></span>
                                </div>
                                <div class="form-group has-feedback">
                                    <label class="control-label" for="lastname">Last Name</label>
                                    <input type="text" class="form-control" placeholder="Last name" name="lastname"
                                           id="lastname"
                                           {% if user %}value="{{ user.lastname }}" {% endif %}>
                                    <span class="form-control-feedback"></span>
                                </div>
                                <div class="form-group has-feedback">
                                    <label class="control-label" for="email">E-mail address</label>
                                    <input type="email" class="form-control" placeholder="Email" name="email" id="email"
                                           {% if user %}value="{{ user.email }}" {% endif %}>
                                    <span class="form-control-feedback"></span>
                                </div>
                                <div class="form-group has-feedback">
                                    <label class="control-label" for="username">Username</label>
                                    <input type="text" class="form-control" placeholder="Username" name="username"
                                           id="username"
                                           {% if user %}value="{{ user.username }}" {% endif %}
                                           {% if not create %}disabled{% endif %}>
                                    <span class="form-control-feedback"></span>
                                </div>
                                <div class="form-group has-feedback {% if blank_password %}has-error{% endif %}">
                                    <label class="control-label" for="password">Password</label>
                                    <input type="password" class="form-control"
                                           placeholder="Password {% if create %}(Required){% else %}(Leave blank to keep unchanged){% endif %}"
                                           name="password" id="password">
                                    <span class="form-control-feedback"></span>
                                    {% if blank_password %}
                                        <span class="help-block">The password cannot be blank.</span>
                                    {% endif %}
                                </div>
                            </div>
                            <!-- /.card-body -->
                            <div class="card-footer">
                                <button type="submit" class="btn btn-primary" title="{{ action_label }} User">
                                    {{ action_label }} User
                                </button>
                            </div>
                            <!-- /.card-footer -->
                        </div>
                    </form>
                    {% if not create %}
                        {% if user.otp_secret %}
                            <div class="card">
                                <div class="card-header">
                                    <h3 class="card-title">Two Factor Authentication</h3>
                                </div>
                                <div class="card-body">
                                    <p>If two-factor authentication is configured for this user and is causing problems
                                        due to a lost device or
                                        technical issue, it can be disabled here.
                                    </p>
                                    <p>The user will need to reconfigure two-factor authentication, to re-enable it.</p>
                                    <p><strong>Beware: This could compromise security!</strong></p>
                                </div>
                                <div class="card-footer">
                                    <button type="button" class="btn btn-warning button_otp_disable"
                                            id="{{ user.username }}">
                                        Disable Two Factor Authentication
                                    </button>
                                </div>
                            </div>
                        {% endif %}
                    {% endif %}
                </div>
                <!-- /.col -->

                <div class="col-12 col-sm-6 col-lg-8">
                    <div class="card card-outline card-secondary shadow">
                        <div class="card-header">
                            <h3 class="card-title">User Editor Help</h3>
                        </div>
                        <!-- /.card-header -->
                        <div class="card-body">
                            <p>Fill in all the fields to the in the form to the left.</p>
                            {% if create %}
                                <p><strong>Newly created users do not have access to any zones.</strong> You will need
                                    to grant
                                    access to the user once it is created via the zone management buttons on the
                                    dashboard.
                                </p>
                            {% else %}
                                <p><strong>Username</strong> cannot be changed.</p>
                                <p><strong>Password</strong> can be left empty to keep the current password.</p>
                            {% endif %}
                        </div>
                        <!-- /.card-body -->
                    </div>
                    <!-- /.card -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </div>
        <!-- /.container-fluid -->
    </section>
{% endblock %}

{% block extrascripts %}
    <script>
        // handle disabling two-factor authentication
        $(document.body).on('click', '.button_otp_disable', function () {
            var modal = $("#modal_otp_disable");
            var username = $(this).prop('id');
            var info = "Are you sure you want to disable two factor authentication for user " + username + "?";
            modal.find('.modal-body p').text(info);
            modal.find('#button_otp_disable_confirm').click(function () {
                var postdata = {
                    'action': 'user_otp_disable',
                    'data': username,
                    '_csrf_token': '{{ csrf_token() }}'
                }
                applyChanges(postdata, $SCRIPT_ROOT + '/admin/manage-user', false, true);
            })
            modal.modal('show');
        });
    </script>
{% endblock %}

{% block modals %}
    <div class="modal fade modal-warning" id="modal_otp_disable">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Confirmation</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <p></p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">
                        Close
                    </button>
                    <button type="button" class="btn btn-danger float-right" id="button_otp_disable_confirm">
                        Disable Two Factor Authentication
                    </button>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
